import {
	playGdMusic,
	getLyric,
	getInspectMusic
} from '@/http/songs.js';
export default {
	// 暂停和播放状态
	playAudio(context, bool) {
		context.commit('playAudio', bool)
	},
	// 保存歌单的数据
	playMusic(context, data) {
		context.commit('playMusicData', data)
		this.dispatch('play')
	},

	// 当前播放歌曲下标值
	playIndex(context, index) {
		context.commit('playIndex', index)
	},
	// 音频当前播放时长
	changeCurrentTime(context, ctime) {
		context.commit('changeCurrentTime', ctime)
	},
	randomPlayIndex(context) {
		const random = Math.floor(Math.random() * (context.state.playMusicListData.list.length + 1));
		if (random == context.state.playIndex) {
			const random = Math.floor(Math.random() * (context.state.playMusicListData.list.length + 1));
			context.commit('playIndex', random)
			this.dispatch('play')
			return
		}
		context.commit('playIndex', random)
		this.dispatch('play')
	},


	// 下一首
	increasePlayIndex(context) {
		if (context.state.playMusicListData.index == context.state.playMusicListData.list.length - 1) {
			this.dispatch('playIndex', 0);
			this.dispatch('play')
		} else {
			context.commit('increasePlayIndex')
			this.dispatch('play')
		}
	},
	// 上一首
	decreasePlayIndex(context) {
		context.commit('decreasePlayIndex')
		this.dispatch('play')
	},

	// 获取播放歌曲数据
	async play(context) {
		let id = context.state.playMusicListData.list[context.state.playMusicListData.index].id;
		try {
			const checkRes = await getInspectMusic(id)
			if (!checkRes.success) {
				uni.showToast({
					title: checkRes.message,
					icon: 'none',
					duration: 2000
				})
				return
			}
			const {
				data: [playData = {}]
			} = await playGdMusic(id)
			context.commit('playSongUrl', playData.url)
			await this.dispatch('Lyric', id) // 添加await保证歌词加载顺序

		} catch (err) {
			await this.dispatch('Lyric', id) 
			// uni.showToast({
			// 	title: '播放服务异常,即将跳到下一首',
			// 	icon: 'none',
			// 	duration: 2000,
			// 	mask: true
			// })
			if (context.state.audio.playing === false) return
			setTimeout(() => {
				this.dispatch('increasePlayIndex');
			}, 1000)
		} finally {
			uni.hideLoading()
		}
	},
	// 歌词获取
	async Lyric(context, id) {
		const {
			lrc
		} = await getLyric(id);
		if (lrc === undefined) {
			// console.log("没有歌词");
			return context.commit('Lyric', [])
		} else {
			// console.log("有歌词");
			let RegExp = /\[(\d*:\d*\.\d*)\]/;
			let arr = [],
				timeArr = [],
				lyricArr = [],
				mergeArr = [];
			arr = lrc.lyric.split("\n");
			for (let i of arr) {
				/**获取歌词 */
				let lrcs = i.split("]")[1];
				if (lrcs == "" || lrcs == undefined) continue;
				lyricArr.push(lrcs);

				/**处理时间 */
				let resTime = RegExp.exec(i)[1];
				let resTime2 = resTime.split(":");
				let min = parseInt(resTime2[0]) * 60;
				let sec = parseFloat(resTime2[1]);
				let time = parseFloat(Number(min + sec).toFixed(2)); //toFixed返回值是String
				timeArr.push(time);
			}
			for (let i = 0, length = timeArr.length; i < length; i++) {
				let obj = {
					time: timeArr[i],
					lyric: lyricArr[i],
				};
				mergeArr.push(obj);
			}
			context.commit('Lyric', mergeArr)
		}
	},


}